home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 026-050 / scopedisk36 / friend / friends.c < prev    next >
C/C++ Source or Header  |  1995-03-18  |  4KB  |  147 lines

  1.  
  2. /* is your mouse pointer feeling lonely ? try this little program to
  3.    help him out... */
  4.  
  5.  
  6. #include <functions.h>
  7. #include <exec/memory.h>
  8. #include <graphics/gfxbase.h>
  9. #include <graphics/sprite.h>
  10.  
  11. #define AllocChip(size) AllocMem((ULONG)size,(ULONG)(MEMF_CHIP|MEMF_CLEAR))
  12. #define DataBytes       (height*2+4)*2
  13. #define prec 8
  14.  
  15. struct IntuitionBase *IntuitionBase;
  16. struct GfxBase *GfxBase;
  17.  
  18. /* my control window... */
  19.  
  20. struct NewWindow newwin = 
  21. { 0, 30, 160, 10, -1, -1,
  22.   CLOSEWINDOW,
  23.   WINDOWCLOSE | WINDOWDRAG | WINDOWDEPTH | 
  24.   SMART_REFRESH,
  25.   NULL, NULL, (UBYTE *)" Friends!  ", NULL, NULL,
  26.   0, 0, 0, 0,
  27.   WBENCHSCREEN };
  28. struct IntuiMessage *msg;
  29. struct Window *win;
  30.  
  31.  
  32. struct Screen *wb;            /* workbench screen */
  33. struct ViewPort *vp;          /* screen's viewport */
  34. USHORT *clrs;                 /* viewport's colour table */
  35. struct SimpleSprite ss[8];    /* only use 1 to 7 */
  36. struct SimpleSprite *ptr;     /* a copy of pointer sprite */
  37. short height;                 /* pointer height */
  38. short which[8]                /* which sprites i'm using */
  39.   = { 0,0,0,0,0,0,0,0 };
  40. long px[8],py[8];             /* fixed point sprite positions */
  41. long rate = 100;                    /* scale factor for moving */
  42. long rnd = 100;               /* randomness factor */
  43.  
  44. long fixmult(a,b)       /* fixed point multiply */
  45.   long a,b;
  46. { return( a>>(prec/2) * b>>(prec/2) ); }
  47. long tofix(a)        /* convert short a to fixed point */
  48.   short a;
  49. { return( ((long)a) << (prec/2) ); }
  50. short toint(a)       /* convert fixed point a to short */
  51.   long a;
  52. { return( (short)(a>>(prec/2)) ); }
  53.  
  54.  
  55. void init()    /* set up sprite system */
  56. {
  57.   short i,j,any;
  58.   
  59.   IntuitionBase = (void *)OpenLibrary("intuition.library",0L);
  60.   GfxBase = (void *)OpenLibrary("graphics.library",0L);
  61.   wb = IntuitionBase->ActiveWindow->WScreen;
  62.   vp = &wb->ViewPort;
  63.   clrs = (USHORT *)(vp->ColorMap->ColorTable);   /* base of colour table */
  64.   ptr = *GfxBase->SimpleSprites;                 /* get pointer to ptr */
  65.   height = ptr->height;                         /* pointer height */
  66.  
  67.   any = FALSE;
  68.   for (i=1; i<8; i++) {     /* set up sprites */
  69.     if (any |= which[i] = ~GetSprite(&ss[i],(long)i)) {
  70.       ss[i].height = height;
  71.       if (!(ss[i].posctldata = AllocChip(DataBytes))) exit(0);  /* data */
  72.       movmem(ptr->posctldata,ss[i].posctldata,DataBytes);
  73.       ss[i].posctldata[1] = ss[i].posctldata[0] = 0;
  74.       ss[i].posctldata[DataBytes/2-2] = ss[i].posctldata[DataBytes/2-1]
  75.       = 0xffff;
  76.       px[i] = tofix(ss[i].x = (short)RangeRand(640L));    /* set position */
  77.       py[i] = tofix(ss[i].y = (short)RangeRand(200L));
  78.     }
  79.   }
  80.   if (!any) exit(0);       /* stingy bloody user! */
  81.  
  82.   for (i=1; i<=3; i++)
  83.     movmem(&clrs[16],&clrs[16+i*4],4*2);   /* set all sprite colours same */
  84.   LoadRGB4(vp,clrs,32L);
  85.  
  86.   if (!(win=OpenWindow(&newwin))) exit(0);     /* open window */
  87. }
  88.  
  89.  
  90. void move()        /* move all ptr's friends */
  91. {
  92.   static long vx[8] = { 0,0,0,0,0,0,0,0 };
  93.   static long vy[8] = { 0,0,0,0,0,0,0,0 };
  94.   long ax,ay;           /* x and y acceleration (fixed point) */
  95.   long mx,my;           /* mouse x and y (fixed point) */
  96.   short i;
  97.  
  98.   mx = tofix(wb->MouseX); my = tofix(wb->MouseY);
  99.   for (i=1; i<8; i++) {
  100.     if (which[i]) {
  101.       ax = (mx-px[i]) - 2*vx[i] + tofix((short)(RangeRand(3*rnd)-3*rnd/2));
  102.       ay = (my-py[i]) - 2*vy[i] + tofix((short)(RangeRand(rnd)-rnd/2));
  103.       vx[i] += ax/rate;
  104.       vy[i] += ay/rate;
  105.       px[i] += vx[i]; py[i] += vy[i];                /* position */
  106.       if (toint(px[i])<0)   { px[i]=-px[i]; vx[i]=-vx[i]; }
  107.       if (toint(px[i])>639) { px[i]=tofix(639*2)-px[i]; vx[i]=-vx[i]; }
  108.       if (toint(py[i])<0)   { py[i]=-py[i]; vy[i]=-vy[i]; }
  109.       if (toint(py[i])>199) { py[i]=tofix(199*2)-py[i]; vy[i]=-vy[i]; }
  110.       MoveSprite(vp,&ss[i],
  111.                  (long)toint(px[i]),
  112.                  (long)toint(py[i]));
  113.     }
  114.   }
  115. }    
  116.  
  117.  
  118. main(argc,argv)
  119.   int argc; char *argv[];
  120. {
  121.   short i;
  122.  
  123.   if (argv[1]) {
  124.     rate = atol(argv[1]);
  125.     if (argv[2]) rnd  = atol(argv[2]);
  126.   }
  127.   init();
  128.   while (!(msg=(void *)GetMsg(win->UserPort))) {
  129.    WaitTOF();
  130.    move();
  131.   }
  132.   ReplyMsg(msg);
  133.  
  134.   for (i=1; i<8; i++) {
  135.     if (which[i]) {
  136.       FreeSprite((long)i);
  137.       if (ss[i].posctldata)
  138.         FreeMem(ss[i].posctldata,(long)DataBytes);
  139.     }
  140.   }
  141.   CloseWindow(win);
  142.   CloseLibrary(GfxBase);
  143.   CloseLibrary(IntuitionBase);
  144. }
  145.   
  146.   
  147.